package cn.swing.ushare.comment.webapi.service;

import cn.swing.ushare.comment.service.ICommentService;
import cn.swing.ushare.comment.webapi.mapper.CommentMapper;
import cn.swing.ushare.common.ex.ServiceException;
import cn.swing.ushare.common.web.State;
import cn.swing.ushare.pojo.dto.CommentAddNewDTO;
import cn.swing.ushare.pojo.vo.ArticleCommentsVO;
import cn.swing.ushare.pojo.vo.CommentListVO;
import cn.swing.ushare.pojo.vo.CommentMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {
    @Autowired
    CommentMapper mapper;

    @Override
    public Integer insertComment(CommentAddNewDTO comment) {
        return mapper.insertComment(comment);
    }


    @Override
    public Integer updateByIds(Integer[] ids) {
        List<Integer> idList = new ArrayList<>();
        for (Integer id : ids) {
            if(mapper.selectStateById(id) == 0){
                idList.add(id);
            }
        }
        Integer integer = mapper.updateByIds(ids);
        if(integer == 0){
            throw new ServiceException(State.ERR_COMMENT_UPDATE,"更新失败,评论不存在");
        }else{
            for (Integer id : idList) {
                mapper.insertOperationLog("评论管理","更新",logOperation(),"通过了: '" + mapper.selectNicknameById(id) + "'的评论");
            }
            return integer;
        }
    }

    @Override
    public Integer deleteByIds(Integer[] ids) {
        List<String> nicknames = new ArrayList<>();
        for (Integer id : ids) {
            nicknames.add(mapper.selectNicknameById(id));
        }
        Integer integer = mapper.deleteByIds(ids);
        if(integer != 0){
            for (String nickname : nicknames) {
                mapper.insertOperationLog("评论管理","删除",logOperation(),"删除用户: '" + nickname + "'的评论");
            }
            return integer;
        }else{
            throw new ServiceException(State.ERR_COMMENT_DELETE,"删除失败,评论不存在");
        }
    }

    @Override
    public List<CommentListVO> list(String param) {
        //全部
        if(param.equals("all")){
            return mapper.list();
        }
        else if (param.equals("review") || param.equals("normal")){
            //review=审核中  normal=正常
            if(param.equals("review")){
                return mapper.listByState(0);
            }else{
                return mapper.listByState(1);
            }
        }else{
            return mapper.listByNickname(param);
        }
    }


    @Override
    public List<ArticleCommentsVO> getCommentListByArticleId(Integer id) {
        return mapper.getCommentListByArticleId(id);
    }


    @Override
    public List<ArticleCommentsVO> getChildCommentListByArticleId(Integer id) {
        return mapper.getChildCommentListByArticleId(id);
    }

    @Override
    public List<CommentMessageVO> listCommentMessages(Integer id) {
        return mapper.listCommentMessages(id);
    }

    @Override
    public Integer getUnreadMessagesCounts(Integer id) {
        return mapper.getUnreadMessagesCounts(id);
    }

    @Override
    public Integer markIsRead(Integer id) {
        return mapper.markIsRead(id);
    }

    // 日志操作
    private String logOperation(){
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String username = (String) principal;
        return username;
    }
}
